package com.rover.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.grid.DataGrid;
import com.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grid.Page;
import com.rover.system.entity.SysParam;
import com.rover.system.service.SysParamService;

/**
 * 系统参数
 */
@Controller
@RequestMapping("/sysParamController")
public class SysParamController {
	
    @Autowired
    private SysParamService sysParamService;
    
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request,Page page, HttpServletResponse response) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
    	String keyWords = (String) request.getParameter("keyWords");
    	String hql = "";
		if(StringUtils.isNotBlank(keyWords)){
			hql = "where paramName like '%" + keyWords + "%'";
			hql += "or paramVal like '%" + keyWords + "%'";
			hql += "or description like '%" + keyWords + "%'";
			hql += "or paramType like '%" + keyWords + "%'";
		}
		Map<String, Object> params = new HashMap<String, Object>();
        DataGrid dataGrid=   sysParamService.query(page, params, hql);
        map = JsonUtil.JSONString(dataGrid.getResult(),
                dataGrid.getTotalCount());
		return map;
    }
    
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("id") String id) {
    	Map<String, Object> map = new HashMap<String, Object>();
		try {
			sysParamService.delete(Long.valueOf(id));
			map.put("success", true);
			map.put("message", "");
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", e.getMessage());
		}
		return map;
    }
    
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(SysParam param) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	try{
    		sysParamService.save(param);
    		map.put("success", Boolean.TRUE);
    	}catch(Exception e){
    		map.put("success", Boolean.FALSE);
    		map.put("message", "保存失败");
    	}
		return map;
    }
    
}
